package com.ruoyi.web.controller.Packets;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Packets.domain.Packets;
import com.ruoyi.Packets.service.IPacketsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;

/**
 * 红包 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-30
 */
@Controller
@RequestMapping("/Packets/packets")
public class PacketsController extends BaseController
{
    private String prefix = "Packets/packets";
	
	@Autowired
	private IPacketsService packetsService;
	
	@RequiresPermissions("Packets:packets:view")
	@GetMapping()
	public String packets()
	{
	    return prefix + "/packets";
	}
	
	/**
	 * 查询红包列表
	 */
	@RequiresPermissions("Packets:packets:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Packets packets)
	{
		startPage();
        List<Packets> list = packetsService.selectPacketsListForShow(packets);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出红包列表
	 */
	@RequiresPermissions("Packets:packets:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Packets packets)
    {
    	List<Packets> list = packetsService.selectPacketsList(packets);
        ExcelUtil<Packets> util = new ExcelUtil<Packets>(Packets.class);
        return util.exportExcel(list, "packets");
    }
	
	/**
	 * 新增红包
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存红包
	 */
	@RequiresPermissions("Packets:packets:add")
	@Log(title = "红包", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Packets packets)
	{		
		String man=ShiroUtils.getSysUser().getUserName();
		packets.setCreateBy(man);
		packets.setCreactDate(DateTools.getYMDhms());		
		return toAjax(packetsService.insertPackets(packets));
	}

	/**
	 * 修改红包
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Packets packets = packetsService.selectPacketsById(id);
		String createMan=packets.getCreateBy();
		
		mmap.put("packets", packets);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存红包
	 */
	@RequiresPermissions("Packets:packets:edit")
	@Log(title = "红包", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Packets packets)
	{		
		return toAjax(packetsService.updatePackets(packets));
	}
	
	/**
	 * 删除红包
	 */
	@RequiresPermissions("Packets:packets:remove")
	@Log(title = "红包", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(packetsService.deletePacketsByIds(ids));
	}
	
}
